为了账号安全,请及时绑定邮箱和手机立即绑定

Linux-常用命令

标签:
Linux

常用命令集合

文件查找 find、locate

find

find: 文件查找,针对文件名,精确查找,磁盘搜索,io读写,cpu开销大

find [options] [path...] [expression] [action]

b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件
-size n[cwbkMG] : 文件大小 为 n 个由后缀决定的数据块。其中后缀为:
b: 代表 512 位元组的区块(如果用户没有指定后缀,则默认为 b)
c: 表示字节数
k: 表示 kilo bytes (1024字节)
w: 字 (2字节)
M:兆字节(1048576字节)
G: 千兆字节 (1073741824字节)
-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-maxdepth 查找最大目录层数 如 1,即只查找一层目录
-fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件
/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount 在查找文件时不跨越文件系统mount点。
-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

===expression===

按文件名:

[root@localhost ~]# find /etc -name "ifcfg-eth0"[root@localhost ~]# find /etc -iname "ifcfg-eth0"            //-i忽略大小写[root@localhost ~]# find /etc -iname "ifcfg-eth*"

按文件大小:

[root@localhost ~]# find /etc -size +5M                        //大于5M[root@localhost ~]# find /etc -size 5M[root@localhost ~]# find /etc -size -5M[root@localhost ~]# find /etc -size +5M -ls                    //-ls找到的处理动作

指定查找的目录深度:

-maxdepth levels-mindepth levels
[root@localhost ~]# find / -maxdepth 4 -a  -name "ifcfg-eth0"

按时间找(atime,mtime,ctime):

[root@localhost ~]# find /etc -mtime +5                      //修改时间超过5天[root@localhost ~]# find /etc -mtime 5                       //修改时间等于5天[root@localhost ~]# find /etc -mtime -5                      //修改时间5天以内

按文件属主、属组找:

复制代码

[root@localhost ~]# find /home -user jack                    //属主是jack的文件[root@localhost ~]# find /home -group hr                     //属组是hr组的文件[root@localhost ~]# find /home -user jack -group hr[root@localhost ~]# find /home -user jack -a -group hr[root@localhost ~]# find /home -user jack -o -group hr[root@localhost ~]# find /home -nouser[root@localhost ~]# find /home -nogroup[root@localhost ~]# find /home -nouser -o -nogroup

复制代码

按文件类型:

复制代码

[root@localhost ~]# find /dev -type f                           //f普通[root@localhost ~]# find /dev -type d                           //d目录[root@localhost ~]# find /dev -type l                           //l链接[root@localhost ~]# find /dev -type b                           //b块设备[root@localhost ~]# find /dev -type c                           //c字符设备[root@localhost ~]# find /dev -type s                           //s套接字[root@localhost ~]# find /dev -type p                           //p管道文件

复制代码

按文件权限:

复制代码

[root@localhost ~]# find . -perm 644 -ls [root@localhost ~]# find . -perm -644 -ls[root@localhost ~]# find . -perm -600 -ls[root@localhost ~]# find . -perm -222 -ls                            //全局可写[root@localhost ~]# find /usr/bin /usr/sbin -perm -4000 -ls          //包含set uid[root@localhost ~]# find /usr/bin /usr/sbin -perm -2000 -ls          //包含set gid[root@localhost ~]# find /usr/bin /usr/sbin -perm -1000 -ls          //包含sticky

复制代码

按正则表达式:

复制代码

-regex pattern
[root@localhost ~]# find /etc  -regex  '.*ifcfg-eth[0-9]'.*       任意多个字符
[0-9]  任意一个数字

[root@localhost ~]# find /etc -regex '.*ifcfg-enp0s25'/etc/sysconfig/network-scripts/ifcfg-enp0s25

[root@localhost ~]# find /etc -regex '.*ifcfg-enp0s[0-9]+'/etc/sysconfig/network-scripts/ifcfg-enp0s25

复制代码

==找到后处理的动作 ACTIONS: (默认动作-print)==

-print
-ls
-delete
-exec             后面跟自定义的shell命令
-ok                后面跟自定义的shell命令

复制代码

[root@localhost ~]# find /etc -name "ifcfg*"[root@localhost ~]# find /etc -name "ifcfg*" -print[root@localhost ~]# find /etc -name "ifcfg*" -ls[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;[root@localhost ~]# find /etc -name "ifcfg*" -delete扩展知识:find结合xargs
[root@localhost ~]# find . -name "yang*.txt" |xargs rm -rf         [root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

复制代码

find练习

复制代码

1. 将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变# find /etc -type d -exec mkdir /tmp/{} \;2. 将/etc目录复制到/var/tmp/
    将/var/tmp/etc中的所有目录设置权限777(仅目录)
    将/var/tmp/etc中所有文件权限设置为666# cp -rf /etc /var/tmp/ # chmod -R a=rwX /var/tmp/etc/或者
find /var/tmp/etc/ -type d -exec chmod 777 {} \;         //分号是找到一个设置一个权限
find /var/tmp/etc/ -type d -exec chmod 777 {} \+        //加号是统一找到后设置权限
find /var/tmp/etc/ ! -type d -exec chmod 777 {} \+ 

3. 以下命令的区别是什么?
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \+

复制代码

复制代码

[root@localhost ~]# mkdir dir1[root@localhost ~]# touch dir1/file{1..20}[root@localhost ~]# find /root/dir1 -name "file5"[root@localhost ~]# find /root/dir1 ! -name "file5"[root@localhost ~]# find /root/dir1 -name "file5" -o -name "file9" /root/dir1/file5/root/dir1/file9

[root@localhost ~]# find /root/dir1 -name "file5" -o -name "file9" -ls1466515    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 -name "file5" -ls  -o -name "file9" -ls1466499    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file51466515    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -ls1466499    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file51466515    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;removed ‘/root/dir1/file5’
removed ‘/root/dir1/file9’

复制代码

 locate

(查询的数据库: /var/lib/mlocate/mlocate.db)  

计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
   手动更新数据库:updatedb

# locate ifcfg-eth0# locate ifcfg-enp0s25

文件过滤 grep

 grep工具:行过滤

复制代码

OPTIONS:    -i: 不区分大小写    -v: 查找不包含指定内容的行,反向选择    -w: 按单词搜索    -c: 统计匹配到的次数    -n: 显示行号    -r: 逐层遍历目录查找    -A: 显示匹配行及前面多少行    -B: 显示匹配行及后面多少行    -C: 显示匹配行前后多少行    --color=auto :可以将找到的关键词部分加上颜色的显示    -l:只列出匹配的文件名    -L:列出不匹配的文件名    -e: 使用正则搜索    ^key:以什么开头
    key$:以什么结尾

复制代码

每次过滤出来的内容显示颜色:

vim ~/.bashrc
alias grep='grep --color=auto'source ~/.bashrc

 使用方法 >>参考

文件打包压缩 

打包压缩--tar

 tar 建议针对目录,打包压缩多个文件,不会改变文件的属性和权限

用法:

tar optino 打包压缩后的文件,需要打包压缩的文件

选项:

复制代码

-c         创建tar包-f         指定包名-v         显示详细信息-z         使用gzip工具压缩-j         使用bzip2工具压缩-J         使用xz工具压缩-t         查看tar包内容-x         解压tar包-C         指定解压路径-r         追加文件到tar包
说明:
参数前面的“-”可有可无

复制代码

复制代码

# tar -cvf /tmp/DIR.tar dir/        # 将dir目录打包放在/tmp下取名叫DIR.tar
# tar -tf /tmp/DIR.tar            # 查看DIR.tar里面的内容
# tar -r /etc/hosts -f /tmp/DIR.tar    # 追加hosts文件到DIR.tar包里
# tar -tf /tmp/DIR.tar 
# tar -r inittab -f /tmp/DIR.tar 
# tar -tf /tmp/DIR.tar 
# tar -xf /tmp/DIR.tar -C backup/# tar cvzf /tmp/$(date +%F).tar.gz backup/ dir/ test.gz 
# tar -xf /tmp/2017-07-18.tar.gz -C /tmp/aaa/

复制代码

注意:

1、一般情况下,将-f参数放到所有参数的最后面

2、如果往tar包里面追加内容,那么尽可能写相对路径

示例:

复制代码

查阅上述tar包内有哪些文件:# tar -ztvf log.tar.gz由于我们使用 gzip 压缩的log.tar.gz,所以要查阅log.tar.gz包内的文件时,就得要加上z这个选项了。

将tar包解压缩:# tar -zxvf /opt/soft/test/log.tar.gz在预设的情况下,我们可以将压缩档在任何地方解开的

只将tar内的部分文件解压出来:# tar -zxvf /opt/soft/test/log30.tar.gz log2013.log我可以透过tar -ztvf来查阅 tar 包内的文件名称,如果单只要一个文件,就可以透过这个方式来解压部分文件!

文件备份下来,并且保存其权限:# tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log这个-p的属性是很重要的,尤其是当您要保留原本文件的属性时。

在文件夹当中,比某个日期新的文件才备份:# tar -N "2012/11/13" -zcvf log17.tar.gz test备份文件夹内容是排除部分文件:# tar --exclude scf/service -zcvf scf.tar.gz scf/*

复制代码

打包压缩--zip

zip兼容unix和windows,可以压缩多个文件或目录

用法:

压缩

zip [option] 压缩后的文件 需要压缩的文件(可以多个)

解压缩

unzip  需要解压的文件

unzip -d path  需要解压的文件

选项

复制代码

zip 命令:是一个应用广泛的跨平台的压缩工具,压缩文件的后缀为 zip文件-A 自动解压文件-c 给压缩文件加注释-d 删除文件-F 修复损坏文件-k 兼容 DOS-m 压缩完毕后,删除源文件-q 运行时不显示信息处理信息-r 处理指定目录和指定目录下的使用子目录-v 显示信息的处理信息-x “文件列表” 压缩时排除文件列表中指定的文件-y 保留符号链接-b<目录> 指定压缩到的目录-i<格式> 匹配格式进行压缩-L 显示版权信息-t<日期> 指定压缩文件的日期-<压缩率> 指定压缩率

复制代码

练习:

复制代码

1、将/boot、/etc目录下所有的文件压缩到/tmp目录里,叫20170718.zip# zip -r /tmp/$(date +%Y%m%d).zip /boot /etc2、将20170718.zip文件解压到指定目录/backup里# mkdir /backup# unzip /tmp/20170718.zip -d /backup# zip /tmp/$(date +%F).dir dir/# unzip /tmp/2017-07-18.dir -d backup/

复制代码

复制代码

# zip -m myfile.zip ./rpm_info.txt      #向压缩文件中myfile.zip中添加rpm_info.txt文件# zip -r filename.zip file1 file2 file3 /usr/work/school     #多个文件或目录,# -x  排除指定文件的运用;压缩当前文件所有内容,出了 images和upload目录下的所有文件# zip -r back.zip ./* -x "./images/*" -x "./upload/*"

复制代码

打包压缩--gzip

gzip 压缩速度快,压缩率低,CPU开销比较低

用法:

压缩

gzip 需要压缩的文件 [file1 file2 ...]

解压缩

gzip -d 需要解压的文件

gunzip 需要解压的文件

注意:

 保留原文件需要加-r选项

选项

复制代码

 -a或--ascii  使用ASCII文字模式。 
 -c或--stdout或--to-stdout  把解压后的文件输出到标准输出设备。 
 -f或-force  强行解开压缩文件,不理会文件名称或硬连接是否存在以及该文件是否为符号连接。 
 -h或--help  在线帮助。 
 -l或--list  列出压缩文件的相关信息。 
 -L或--license  显示版本与版权信息。 
 -n或--no-name  解压缩时,若压缩文件内含有远来的文件名称及时间戳记,则将其忽略不予处理。 
 -N或--name  解压缩时,若压缩文件内含有原来的文件名称及时间戳记,则将其回存到解开的文件上。 
 -q或--quiet  不显示警告信息。 
 -r或--recursive  递归处理,将指定目录下的所有文件及子目录一并处理。 
 -S<压缩字尾字符串>或--suffix<压缩字尾字符串>  更改压缩字尾字符串。 
 -t或--test  测试压缩文件是否正确无误。 
 -v或--verbose  显示指令执行过程。 

 -V或--version 显示版本信息。

复制代码

复制代码

# tar cf test.tar -R test   #gzip不能压缩目录,先打包# gzip test.tar             #压缩# gzip -l test.tar.gz       #查看压缩包中的内容# gzip -dv test.tar.gz     #解压# gzip -rv /var/www    #递归的压缩目录# gzip -dr test6     # 递归地解压目录

复制代码

打包压缩--bzip2

bzip2 压缩速度慢 压缩率高 CPU开销大

用法:

压缩

bzip2 需要压缩的文件

解压缩

bzip2 -d 需要解压的文件

bunzip2 需要解压的文件

选项

复制代码

-c --stdout
    将数据压缩或解压缩至标准输出。-d --decompress
    强制解压缩。 bzip2, bunzip2 以及 bzcat 实际上是同一个程序,进行何种操作将根据程序名确定。 指定该选项后将不考虑这一机制,强制 bzip2 进行解压缩。-z --compress-d 选项的补充:强制进行压缩操作,而不管执行的是哪个程序。-t --test
    检查指定文件的完整性,但并不对其解压缩。 实际上将对数据进行实验性的解压缩操作,而不输出结果。-f --force
    强制覆盖输出文件。通常 bzip2 不会覆盖已经存在的文件。该选项还强制 bzip2 打破文件的硬连接,缺省情况下 bzip2 不会这么做。-k --keep
    在压缩或解压缩时保留输入文件(不删除这些文件)。-s --small
    在压缩、 解压缩及检查时减少内存用量。 采用一种修正的算法进行压缩和测试, 每个数据块仅需要 2.5 个字节。 这意味着任何文件都可以在 2300k 的内存中进行解压缩, 尽管速度只有通常情况下的一半。
    在压缩时,-s将选定 200k 的块长度,内存用量也限制在 200k 左右, 代价是压缩率会降低。 总之,如果机器的内存较少(8兆字节或更少), 可对所有操作都采用-s选项。参见下面的内存管理。-q --quiet
    压制不重要的警告信息。属于 I/O 错误及其它严重事件的信息将不会被压制。-v --verbose
    详尽模式 -- 显示每个被处理文件的压缩率。 命令行中更多的 -v 选项将增加详细的程度, 使 bzip2 显示出许多主要用于诊断目的信息。-L --license -V --version
    显示软件版本,许可证条款及条件。-1 to -9
    在压缩时将块长度设为 100 k、200 k .. 900 k。 对解压缩没有影响。参见下面的内存管理。--
    将所有后面的命令行变量看作文件名,即使这些变量以减号"-"打头。 可用这一选项处理以减号"-"打头的文件名, 例如:bzip2 -- -myfilename.--repetitive-fast --repetitive-best
    这些选项在 0.9.5 及其以上版本中是多余的。 在较早的版本中,这两个选项对排序算法 的行为提供了一些粗糙的控制,有些情况下很有用。 0.9.5 及其以上版本采用了改进的算法而与这些选项无关

复制代码

复制代码

# bzip2 -z abc.sh           #压缩# bzip2 -kv abc.sh          #压缩原文保留# bzip2 -9 -c abc.sh >abc.bz2       #压缩原文保留# bzip2 -tv test.bz2     # 模拟解压# bzip2 -k test          # 生成新文件,原文件也保留# bzip2 -dc test.bz2     # 解压到标准输出

复制代码

打包压缩--xz

xz 高压缩率, 解压速度快 但是压缩时间较长,CPU开销相对较大

用法:

压缩

xz 需要压缩的文件

解压缩

xz -d 需要解压的文件

unxz 需要解压的文件

 

 

 

 

 未完....            待续

原文出处:https://www.cnblogs.com/yanjieli/p/9543329.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消